package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.query.StrategyThemeQuery;
import cn.wolfcode.luowowo.repository.StrategyThemeRepository;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.DBHelper;
import cn.wolfcode.luowowo.util.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

//攻略主题业务实现类
@Service
public class StrategyThemeServiceImpl implements IStrategyThemeService {
    //注入攻略主题数据接口
    @Autowired
    private StrategyThemeRepository strategyThemeRepository;
    @Autowired
    private MongoTemplate mongoTemplate;


    //根据攻略主题id查询攻略主题信息
    @Override
    public StrategyTheme get(String id) {
        return strategyThemeRepository.findById(id).orElse(null);
    }

    @Override
    public void save(StrategyTheme strategyTheme) {
        MyUtils.hasLength(strategyTheme.getName() , "名称不能为空");
        MyUtils.isSequence(strategyTheme.getSequence());
        //防止调用者传了id覆盖了数据
        strategyTheme.setId(null);
        strategyThemeRepository.save(strategyTheme);
    }

    @Override
    public void update(StrategyTheme strategyTheme) {
        MyUtils.hasLength(strategyTheme.getId() , "编辑的id不能为空");
        MyUtils.hasLength(strategyTheme.getName() , "名称不能为空");
        MyUtils.isSequence(strategyTheme.getSequence());
        strategyThemeRepository.save(strategyTheme);
    }

    @Override
    public void delete(String id) {
        strategyThemeRepository.deleteById(id);
    }

    //分页查询
    @Override
    public Page<StrategyTheme> list(StrategyThemeQuery qo) {
        //创建条件对象
        Query query = new Query();
        //调用数据库工具类查询
        return DBHelper.PagingQuery(mongoTemplate, query, StrategyTheme.class,qo.getPageable());
    }
    //获取所有主题
    @Override
    public List<StrategyTheme> allList() {
        return strategyThemeRepository.findAll();
    }
}
